package com.lanou.horo.zhuizhui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.entity.BackGroundItem;
import com.lanou.horo.zhuizhui.utility.clickInterface.BackgroundItemLisitener;
import com.lanou.horo.zhuizhui.utility.imageLoader.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/10.
 */
public class BackGroundAdapter extends RecyclerView.Adapter<BackGroundAdapter.BackGroundViewHolder> {
    private List<BackGroundItem.DataEntity> entities;
    private ImageLoader imageLoader;
    private Context context;
    private BackgroundItemLisitener itemLisitener;

    public List<BackGroundItem.DataEntity> getEntities() {
        return entities;
    }

    public BackGroundAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();

    }
    public void addPiclist(List<BackGroundItem.DataEntity> picArrEntities){
        entities = picArrEntities;
        notifyDataSetChanged();
    }
    public void setItemLisitener(BackgroundItemLisitener bil) {
        this.itemLisitener = bil;
    }

    @Override
    public BackGroundViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.activity_background_item, null);
        return new BackGroundViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BackGroundViewHolder holder, int position) {
        holder.itemPosition = position;
        if (entities != null && entities.size() > 0) {
            BackGroundItem.DataEntity bgd = entities.get(position);
            String url = bgd.getImage().getSmall();
            holder.backgroundNetWorkimg.setImageUrl(url, imageLoader);
            Log.d("BackGroundAdapter", url);
        }
    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }

    public class BackGroundViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView backgroundNetWorkimg;
        private int itemPosition;

        public BackGroundViewHolder(View itemView) {
            super(itemView);
            backgroundNetWorkimg = (NetworkImageView) itemView.findViewById(R.id.backgroundNetWorkimg);
            backgroundNetWorkimg.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (entities != null) {
                BackGroundItem.DataEntity bd = entities.get(itemPosition);
                if (itemLisitener != null){
                    itemLisitener.backgroundItemClick(bd);
                }
            }
        }
    }
}
